Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic on RUF027 #9990

Merged
merged 3 commits into from
Feb 16, 2024
Merged

Fix panic on RUF027 #9990

merged 3 commits into from
Feb 16, 2024

Conversation

snowsignal
Copy link
Contributor

@snowsignal snowsignal commented Feb 14, 2024

Summary

Fixes #9895

The cause for this panic came from an offset error in the code. When analyzing a hypothetical f-string, we attempt to re-parse it as an f-string, and use the AST data to determine, among other things, whether the format specifiers are correct. To determine the 'correctness' of a format specifier, we actually have to re-parse the format specifier, and this is where the issue lies. To get the source text for the specifier, we were taking a slice from the original file source text... even though the AST data for the specifier belongs to the standalone parsed f-string expression, meaning that the ranges are going to be way off. In a file with Unicode, this can cause panics if the slice is inside a char boundary.

To fix this, we now slice from the temporary source we created earlier to parse the literal as an f-string.

Test Plan

The RUF027 snapshot test was amended to include a string with format specifiers which we should be calling out. This is to ensure we do slice format specifiers from the source text correctly.

@snowsignal snowsignal added the bug Something isn't working label Feb 14, 2024
Copy link
Contributor

github-actions bot commented Feb 14, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+1 -0 violations, +0 -0 fixes in 1 projects; 42 projects unchanged)

pandas-dev/pandas (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ pandas/io/formats/format.py:1919:26: RUF027 Possible f-string without an `f` prefix

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF027 1 1 0 0 0

@snowsignal snowsignal enabled auto-merge (squash) February 16, 2024 19:57
@snowsignal snowsignal merged commit 20217e9 into main Feb 16, 2024
17 checks passed
@snowsignal snowsignal deleted the jane/ruf027/slice-panic-fix branch February 16, 2024 20:04
nkxxll pushed a commit to nkxxll/ruff that referenced this pull request Mar 10, 2024
## Summary

Fixes astral-sh#9895 

The cause for this panic came from an offset error in the code. When
analyzing a hypothetical f-string, we attempt to re-parse it as an
f-string, and use the AST data to determine, among other things, whether
the format specifiers are correct. To determine the 'correctness' of a
format specifier, we actually have to re-parse the format specifier, and
this is where the issue lies. To get the source text for the specifier,
we were taking a slice from the original file source text... even though
the AST data for the specifier belongs to the standalone parsed f-string
expression, meaning that the ranges are going to be way off. In a file
with Unicode, this can cause panics if the slice is inside a char
boundary.

To fix this, we now slice from the temporary source we created earlier
to parse the literal as an f-string.

## Test Plan

The RUF027 snapshot test was amended to include a string with format
specifiers which we _should_ be calling out. This is to ensure we do
slice format specifiers from the source text correctly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule RUF027 cause panic
3 participants